package com.k2data.k2app.service.pas04;


import com.k2data.k2app.HttpClientUtil;
import com.k2data.k2app.common.K2Assert;
import com.k2data.k2app.domain.pasObject.PasProject;
import com.k2data.k2app.domain.vo.Job;
import com.k2data.k2app.util.DateFormatterUtil;
import com.k2data.k2app.util.JsonUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 从pas取回来的数据
 * Created by clb on 17-3-30.
 */
@Log4j2
@Service
public class Pas04JobService extends Pas04Basic {
	@Autowired
	private Pas04ProjectService pas04ProjectService;

	public void delete(Long projectId,Long jobId) throws Exception{
		String infoError = HttpClientUtil.onDelete(getJobsUrl()+"?projId="+projectId+"&jobId="+jobId);
		log.warn(String.format("delete job id:%s ,return null!",jobId));
		Map<String, Object> resultMap = JsonUtils.convertJsonStrToMap(infoError);
		K2Assert.isExceptionByPas(resultMap);
	}
	/**
	 * 获取jobs
	 * [条件查询]
	 * @param condition 拼接条件
	 * @return
	 */
	public List<Job>  getJobsByParam (String condition) throws Exception{
		List<Job> jobs = new ArrayList<>();
		List list = getJobListFromPas(condition);
		if(list == null){
			return jobs;
		}
		for(int i =0 ;i<list.size();i++){
			Job jobObject  = resultToJob(list.get(i).toString());
			if(jobObject != null){
				/**
				 * 模板 项目信息
				 */
				String[] infos = jobObject.getProjName().split("_");
				if(infos.length>2){
					String proId = infos[1];
					jobObject.setModelIdTemplate(proId);
					Optional<PasProject> pasProject = Optional.ofNullable(pas04ProjectService.getPasProject(Long.parseLong(proId)));
					pasProject.ifPresent(s->jobObject.setProjNameTemplate(s.getProjName()));
				}
				jobs.add(jobObject);
			}
		}
		return jobs;
	}

	/**
	 * 根据条件查询pas的job
	 * @param condition
	 * @return
	 * @throws Exception
	 */
	public List getJobListFromPas(String condition) throws Exception {
		String infoError = getFromPas(getJobsUrl()+"?"+condition);
		Map<String, Object> resultMap = JsonUtils.convertJsonStrToMap(infoError);

		Optional<Object> optional = Optional.ofNullable(resultMap.get("result"));
		if(optional.isPresent()) {
			List result = JsonUtils.convertJsonStrToList(resultMap.get("result").toString());
			return result;
		}
		return null;
	}

	/**
	 * json 转为  job对象
	 * @param json
	 * @return
	 */
	public Job resultToJob(String json)  throws Exception{
		Map<String, Object> jobMap = JsonUtils.convertJsonStrToMap(json);
		Map<String, Object> instance = JsonUtils.convertJsonStrToMap(jobMap.get("instance"));
		Map<String, Object> project = JsonUtils.convertJsonStrToMap(jobMap.get("project"));
		Map<String, Object> taskInstance = JsonUtils.convertJsonStrToMap(instance.get("taskInstance"));
		String jobId = instance.get("jobId").toString();
		String projId = project.get("projId").toString();
		String projName = project.get("projName").toString();
		String startTime = taskInstance.get("startTime").toString();
		String endTime = taskInstance.get("endTime")==null?String.valueOf(System.currentTimeMillis()):taskInstance.get("endTime").toString();
		String status = taskInstance.get("status")==null?"":taskInstance.get("status").toString();
		String resultStatus = taskInstance.get("result")==null?"":taskInstance.get("result").toString();
		String message = taskInstance.get("message")==null?"":taskInstance.get("message").toString();
		String hdfsPath = instance.get("resultHdfsUrl") == null?"":instance.get("resultHdfsUrl").toString();
		String hasApi = instance.get("hasApi") == null?"":instance.get("hasApi").toString();
		String resultApiUrl = instance.get("resultApiUrl") == null?"":instance.get("resultApiUrl").toString();
		String actualStmtWhere = instance.get("actualStmtWhere") == null?"":instance.get("actualStmtWhere").toString();
		Date startTime1 = new Date(Long.parseLong(startTime));
		Date endTime1 = new Date(Long.parseLong(endTime));
		LocalDateTime start = DateFormatterUtil.UDateToLocalDateTime(startTime1);
		LocalDateTime end = DateFormatterUtil.UDateToLocalDateTime(endTime1);

		Job job = new Job();
//		if(!"".equals(actualStmtWhere)&&actualStmtWhere.contains("deviceNo")){
//			job.setDeviceNo(getDeviceNoFromCondition(actualStmtWhere));
//		}
		job.setJobId(Long.parseLong(jobId));
		job.setModelId(projId);
		job.setProjName(projName);
		job.setStatus(status);
		job.setStartTime(start);
		job.setEndTime(end);
		job.setHdfsPath(hdfsPath);
		job.setResultStatus(resultStatus);
		job.setMessage(message);
		job.setHasApi(hasApi);
		job.setResultApiUrl(resultApiUrl);
		job.setActualStmtWhere(actualStmtWhere);
		return job;
	}
}
